#!/bin/bash

# 伞控无人机日志解析测试脚本
# 展示如何使用新的日志格式进行数据提取和分析

LOG_FILE="flight_log.txt"

echo "=== 伞控无人机日志解析测试 ==="
echo "日志文件: $LOG_FILE"
echo

# 检查日志文件是否存在
if [ ! -f "$LOG_FILE" ]; then
    echo "警告: 日志文件 $LOG_FILE 不存在"
    echo "请确保程序运行并生成了日志文件"
    exit 1
fi

echo "1. 按模块分类统计日志条数:"
echo "================================"
cut -d' ' -f3 "$LOG_FILE" | sort | uniq -c | sort -nr
echo

echo "2. 提取GPS位置数据 (前10条):"
echo "================================"
grep "\[GPS_POS\]" "$LOG_FILE" | head -10
echo

echo "3. 提取IMU姿态数据 (前10条):"
echo "================================"
grep "\[IMU_ATT\]" "$LOG_FILE" | head -10
echo

echo "4. 提取风速风向数据 (前10条):"
echo "================================"
grep "\[WND_SPD\]\|\[WND_DIR\]" "$LOG_FILE" | head -10
echo

echo "5. 提取温湿度数据 (前10条):"
echo "================================"
grep "\[TMP_VAL\]\|\[HMD_VAL\]" "$LOG_FILE" | head -10
echo

echo "6. 提取系统状态信息 (前10条):"
echo "================================"
grep "\[SYS_STA\]\|\[CON_STA\]\|\[SEN_STA\]" "$LOG_FILE" | head -10
echo

echo "7. 提取性能统计数据 (前10条):"
echo "================================"
grep "\[PERF\]" "$LOG_FILE" | head -10
echo

echo "8. 提取错误统计数据:"
echo "================================"
grep "\[ERR_STAT\]" "$LOG_FILE"
echo

echo "9. 按时间范围提取数据 (最近1小时的GPS数据):"
echo "================================"
# 获取当前时间前1小时的时间戳
one_hour_ago=$(date -d '1 hour ago' '+%Y-%m-%d %H:%M:%S')
echo "提取时间范围: $one_hour_ago 至现在"
awk -v start_time="$one_hour_ago" '$1" "$2 >= start_time' "$LOG_FILE" | grep "\[GPS_POS\]" | head -5
echo

echo "10. 生成数据文件:"
echo "================================"

# 生成GPS数据CSV文件
echo "生成GPS位置数据文件: gps_positions.csv"
grep "\[GPS_POS\]" "$LOG_FILE" | grep -o "lat=[0-9.-]*,lon=[0-9.-]*,alt=[0-9.-]*" > gps_positions.csv
echo "GPS数据条数: $(wc -l < gps_positions.csv)"

# 生成风速数据文件
echo "生成风速数据文件: wind_speed.txt"
grep "\[WND_SPD\]" "$LOG_FILE" | grep -o "speed=[0-9.]*" | sed 's/speed=//' > wind_speed.txt
echo "风速数据条数: $(wc -l < wind_speed.txt)"

# 生成温度数据文件
echo "生成温度数据文件: temperature.txt"
grep "\[TMP_VAL\]" "$LOG_FILE" | grep -o "temperature=[0-9.-]*" | sed 's/temperature=//' > temperature.txt
echo "温度数据条数: $(wc -l < temperature.txt)"

# 生成IMU姿态数据文件
echo "生成IMU姿态数据文件: imu_attitude.csv"
grep "\[IMU_ATT\]" "$LOG_FILE" | grep -o "roll=[0-9.-]*,pitch=[0-9.-]*,yaw=[0-9.-]*" > imu_attitude.csv
echo "IMU姿态数据条数: $(wc -l < imu_attitude.csv)"

echo

echo "11. 数据统计分析:"
echo "================================"

# 统计各类传感器数据条数
echo "传感器数据统计:"
echo "  GPS位置数据: $(grep -c "\[GPS_POS\]" "$LOG_FILE")"
echo "  GPS速度数据: $(grep -c "\[GPS_VEL\]" "$LOG_FILE")"
echo "  IMU姿态数据: $(grep -c "\[IMU_ATT\]" "$LOG_FILE")"
echo "  风速数据: $(grep -c "\[WND_SPD\]" "$LOG_FILE")"
echo "  风向数据: $(grep -c "\[WND_DIR\]" "$LOG_FILE")"
echo "  温度数据: $(grep -c "\[TMP_VAL\]" "$LOG_FILE")"
echo "  湿度数据: $(grep -c "\[HMD_VAL\]" "$LOG_FILE")"
echo "  拉力数据: $(grep -c "\[FRC_VAL\]" "$LOG_FILE")"

echo
echo "控制指令统计:"
echo "  电机速度指令: $(grep -c "\[MOT_SPD\]" "$LOG_FILE")"
echo "  舵机角度指令: $(grep -c "\[SRV_ANG\]" "$LOG_FILE")"
echo "  降落伞释放指令: $(grep -c "\[PAR_REL\]" "$LOG_FILE")"

echo
echo "系统状态统计:"
echo "  系统状态记录: $(grep -c "\[SYS_STA\]" "$LOG_FILE")"
echo "  连接状态记录: $(grep -c "\[CON_STA\]" "$LOG_FILE")"
echo "  传感器状态记录: $(grep -c "\[SEN_STA\]" "$LOG_FILE")"

echo
echo "性能和错误统计:"
echo "  性能统计记录: $(grep -c "\[PERF\]" "$LOG_FILE")"
echo "  错误统计记录: $(grep -c "\[ERR_STAT\]" "$LOG_FILE")"

echo
echo "12. 模块活跃度分析:"
echo "================================"
echo "各模块日志条数排序:"
cut -d' ' -f3 "$LOG_FILE" | sort | uniq -c | sort -nr | head -10

echo
echo "=== 测试完成 ==="
echo "生成的数据文件:"
echo "  - gps_positions.csv: GPS位置数据"
echo "  - wind_speed.txt: 风速数据"
echo "  - temperature.txt: 温度数据"
echo "  - imu_attitude.csv: IMU姿态数据"
echo
echo "您可以使用这些文件进行进一步的数据分析和可视化" 